import java.io.*;

public class StudentManager {

    public Student[] students;
    private int[] averange = new int[6];
    private int num = 0;

    public int getNum() {
        return num;
    }

    public void count(String path) {
        File name = new File(path);
        if (name.exists() && name.isFile()) {

        } else {
            System.out.println("未找到学生名单");
        }

        try {
            File is = new File(path);

            BufferedReader br = new BufferedReader(new FileReader(is));
            while (br.readLine() != null) {

                num++;
            }
            num = num - 1;
        } catch (FileNotFoundException e) {
            System.out.println("找不到文件" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readStudents(String path) {
        count(path);
        File name = new File(path);
        if (name.exists() && name.isFile()) {
            System.out.println("从students.txt中获取学生名单");
        } else {
            System.out.println("未找到学生名单");
        }

        try {
            File is = new File(path);

            BufferedReader br = new BufferedReader(new FileReader(is));


            students = new Student[num];
            for (int i = 0; i < num; i++) {
                students[i] = new Student();
            }
            long[] ID = new long[num];
            String[] n = new String[num];
            int k = 0;
            String s = br.readLine();
            while ((s = br.readLine()) != null) {


                String ns = s.substring(0, 13);
                try {
                    ID[k] = Long.parseLong(ns);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                n[k] = s.substring(14);
                students[k].setStudent(ID[k], n[k]);

                k++;
            }

        } catch (FileNotFoundException e) {
            System.out.println("找不到文件" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setAverange() {
        for (int i = 0; i < 6; i++) {
            averange[0] = 0;
        }

        for (int i = 0; i < num; i++) {
            averange[0] += students[i].getChinese();
        }
        for (int i = 0; i < num; i++) {
            averange[1] += students[i].getMath();
        }
        for (int i = 0; i < num; i++) {
            averange[2] += students[i].getEnglish();
        }
        for (int i = 0; i < num; i++) {
            averange[3] += students[i].getPhysics();
        }
        for (int i = 0; i < num; i++) {
            averange[4] += students[i].getChemistry();
        }
        for (int i = 0; i < num; i++) {
            averange[5] += students[i].getGrades();
        }
        for (int i = 0; i < 6; i++) {
            averange[i] /= num;
        }
    }

    public void writeStudents(String path) {
        File data = new File(path);
        if (data.exists() && data.isFile()) {
            System.out.println("使用已经存在的scores.txt");
        } else {
            try {
                data.createNewFile();
                System.out.println("创建scores.txt文件");

            } catch (IOException e) {
                System.out.println("创建失败，错误信息" + e.getMessage());
                return;
            }
        }
        try {
            FileWriter pw = new FileWriter(data, true);
            setAverange();
            for (int i = 0; i < num; i++) {
                pw.write("\n");
                pw.write(String.valueOf(students[i].getID()));
                pw.write("\t");
                pw.write(students[i].getName());
                if (students[i].getName().length() == 2) {
                    pw.write("  ");
                }
                pw.write("\t\t");
                pw.write(String.valueOf(students[i].getChinese()));
                pw.write("\t\t");
                pw.write(String.valueOf(students[i].getMath()));
                pw.write("\t\t");
                pw.write(String.valueOf(students[i].getEnglish()));
                pw.write("\t\t");
                pw.write(String.valueOf(students[i].getPhysics()));
                pw.write("\t\t");
                pw.write(String.valueOf(students[i].getChemistry()));
                pw.write("\t\t");
                pw.write(String.valueOf(students[i].getGrades()));
                pw.write("\n");
            }
            pw.write("                ");

            pw.write("平均值");
            pw.write("\t\t");
            for (int i = 0; i < 6; i++) {
                pw.write(String.valueOf(averange[i]));
                pw.write("\t\t");
            }
            pw.write("\t\t");
            pw.close();
            System.out.println("名单录入成功！" + num + "人");
        } catch (FileNotFoundException e) {
            System.out.println("找不到文件" + e.getMessage());
        } catch (IOException e) {
            System.out.println("录入名单错误" + e.getMessage());
        }
    }

    public void setOrder() {
        for (int i = 0; i < num; i++) {
            for (int j = i; j < num; j++) {
                if (students[i].getGrades() < students[j].getGrades()) {
                    Student a = new Student();
                    a = students[j];
                    students[j] = students[i];
                    students[i] = a;

                }
            }
        }
    }
}
